package models;

import play.*;
import play.data.validation.MaxSize;
import play.db.jpa.*;

import javax.persistence.*;

import flexjson.JSONSerializer;

import java.util.*;

/**
 * Role entity 
 * 角色  1：系统管理员；2：县公司项目专工；3：县公司项目管理；
         4：本部项目专工 5：本部项目管理 6：本部
 */

@Entity 
public class Role extends Model {

    public String name;

    public String remark;
    
    public String toString() {
        return name;
    }
    
    public Role(){}
    public Role(String name,String remark){
    	this.name = name;
    	this.remark = remark;
    }
    
    public static String toJson(List<Role> roles){
    	return new JSONSerializer()
    		.include("id","name","remark")
    		.exclude("*").serialize(roles);
    }
    
    public static String toJson(Role role){
    	return new JSONSerializer()
		.include("id","name","remark")
		.exclude("*").serialize(role);
    }
    
    public static List<Role> findByUser(User u){
    	List<UserRole> urs = UserRole.findByUser(u);
    	List<Role> rs = new ArrayList<Role>();
    	for(UserRole ur : urs){
    		rs.add(ur.role);
    	}
    	return rs;
    }

}
